package com.bwda.dsrs.systemservice.service.impl;

import com.bwda.dsrs.systemservice.domain.Constants;
import com.bwda.dsrs.systemservice.domain.condition.menu.MenuCondition;
import com.bwda.dsrs.systemservice.domain.form.menu.MenuTree;
import com.bwda.dsrs.systemservice.domain.form.menu.MenuTreeForm;
import com.bwda.dsrs.systemservice.domain.po.menu.MenuPo;
import com.bwda.dsrs.systemservice.mapper.MenuMapper;
import com.bwda.dsrs.systemservice.service.MenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单管理Service实现层
 *
 * @author zhangyinmei
 * @date 2019年5月7日
 */
@Service
public class MenuServiceImpl extends AbstractBaseServiceImpl<MenuPo, MenuCondition, MenuMapper> implements MenuService {


    /**
     * 数据转换Tree
     */
    private List<MenuTreeForm> listGetStree(List<MenuTreeForm> list) {
        List<MenuTreeForm> treeList = new ArrayList<MenuTreeForm>();
        for (MenuTreeForm tree : list) {
            if (tree.getParentMenuId() == 0) {
                treeList.add(tree);
            }
            for (MenuTreeForm treeNode : list) {
                if (treeNode.getParentMenuId() == tree.getId()) {
                    if (tree.getChildren() == null) {
                        tree.setChildren(new ArrayList<MenuTreeForm>());
                    }
                    tree.getChildren().add(treeNode);
                }
            }
        }
        return treeList;
    }

    private List<MenuTree> listGetMenuStree(List<MenuTreeForm> list) {
        List<MenuTree> treeList = new ArrayList<MenuTree>();
        MenuTree menuTree;
        MenuTree treeNodes;
        for (MenuTreeForm tree : list) {
            MenuCondition mc = new MenuCondition();
            mc.setParentMenuId(tree.getParentMenuId());
            int pcount = this.queryCount(mc);
            menuTree = new MenuTree();
            menuTree.setId(tree.getId());
            menuTree.setName(tree.getMenuName());
            menuTree.setPath(tree.getMenuUrl());
            menuTree.setIcon(tree.getIcon());
            menuTree.setIsShow(tree.getIsShow());
            menuTree.setOrderNum(tree.getOrderNum());
            if (tree.getOrderNum() == 1 && tree.getOrderNum() == pcount) {
                menuTree.setMoveFlag(Constants.INDEX_UNDO);
            } else {
                if (tree.getOrderNum() == 1) {
                    menuTree.setMoveFlag(Constants.INDEX_FIRST);
                }
                if (tree.getOrderNum() == pcount) {
                    menuTree.setMoveFlag(Constants.INDEX_LAST);
                }
            }
            if (tree.getParentMenuId() == 0) {
                treeList.add(menuTree);
            }
            for (MenuTreeForm treeNode : list) {
                if (treeNode.getParentMenuId() == tree.getId()) {
                    MenuCondition mcc = new MenuCondition();
                    mcc.setParentMenuId(treeNode.getParentMenuId());
                    int childcount = this.queryCount(mcc);
                    if (menuTree.getChildren() == null) {
                        menuTree.setChildren(new ArrayList<MenuTree>());
                    }
                    treeNodes = new MenuTree();
                    treeNodes.setId(treeNode.getId());
                    treeNodes.setIcon(treeNode.getIcon());
                    treeNodes.setPath(treeNode.getMenuUrl());
                    treeNodes.setName(treeNode.getMenuName());
                    treeNodes.setIsShow(treeNode.getIsShow());
                    treeNodes.setOrderNum(treeNode.getOrderNum());
                    if (treeNode.getOrderNum() == childcount && treeNode.getOrderNum() == 1) {
                        treeNodes.setMoveFlag(Constants.INDEX_UNDO);
                    } else {
                        if (treeNode.getOrderNum() == 1) {
                            treeNodes.setMoveFlag(Constants.INDEX_FIRST);
                        }
                        if (treeNode.getOrderNum() == childcount) {
                            treeNodes.setMoveFlag(Constants.INDEX_LAST);
                        }
                    }
                    menuTree.getChildren().add(treeNodes);
                }

            }
            if (menuTree.getChildren() != null && !menuTree.getChildren().isEmpty()) {
                menuTree.setChildSize(menuTree.getChildren().size());
            }
        }
        return treeList;
    }


    private List<MenuTree> listGetMenuStreeSingle(List<MenuTreeForm> list) {
        List<MenuTree> treeList = new ArrayList<MenuTree>();
        MenuTree menuTree;
        for (MenuTreeForm tree : list) {
            MenuCondition mc = new MenuCondition();
            mc.setParentMenuId(tree.getParentMenuId());
            menuTree = new MenuTree();
            menuTree.setId(tree.getId());
            menuTree.setName(tree.getMenuName());
            menuTree.setPath(tree.getMenuUrl());
            menuTree.setIcon(tree.getIcon());
            menuTree.setIsShow(tree.getIsShow());
            menuTree.setOrderNum(tree.getOrderNum());
            treeList.add(menuTree);
        }
        return treeList;
    }

    @Override
    public List<MenuTreeForm> queryTree(List<MenuTreeForm> poList) {
        return this.listGetStree(poList);
    }

    @Override
    public List<MenuTree> queryMenuTree(List<MenuTreeForm> poList) {
        return this.listGetMenuStree(poList);
    }

    @Override
    public List<MenuTree> queryMenuTreeSingle(List<MenuTreeForm> poList) {
        return this.listGetMenuStreeSingle(poList);
    }

    @Override
    public int countByName(MenuCondition condition) {
        return this.getMapper().countByName(condition);
    }

    @Override
    public List<MenuTreeForm> queryTreeList(MenuCondition menuCondition) {
        return this.getMapper().queryTreeList(menuCondition);
    }

    /**
     * 查询统计排序相邻的菜单
     *
     * @param condition
     * @return
     */
    @Override
    public MenuPo queryNearMenu(MenuCondition condition) {
        return this.getMapper().selectNearMenu(condition);
    }

    /**
     * 交换排序
     *
     * @param mecondition menuCurrent
     *                    menuNext
     */
    @Override
    public int exchangeSortNum(MenuCondition mecondition) {
        return this.getMapper().exchangeSortNum(mecondition);
    }

}